package com.autumn.xa.a.tuling.jvm;

/**
 * ClassName: DeadLockTest
 *
 * @Description:
 * @Author: wangqiuyang
 * @Date: 2022/7/2 1:58 PM
 * History:
 * <author>          <time>          <version>          <desc>
 * wangqiuyang    2022/7/2 1:58 PM   @Version 1.0        描述
 */
public class DeadLockTest {

    // 手动写一个死锁程序
    private static Object lock1 = new Object();
    private static Object lock2 = new Object();

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (lock1) {
                try {
                    System.out.println("thread1 begin");
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                }
                synchronized (lock2) {
                    System.out.println("thread1 end");
                }
            }
        }).start();

        new Thread(() -> {
            synchronized (lock2) {
                try {
                    System.out.println("thread2 begin");
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                }
                synchronized (lock1) {
                    System.out.println("thread2 end");
                }
            }
        }).start();

        System.out.println("main thread end");
    }
}
